SET SERVEROUTPUT ON
DECLARE
--Creating the variables for storing the cursor details.
username user_action_log.username%type;
Seconds varchar2(20);
Minutes varchar2(20);
Hours varchar2(20);
Days varchar2(20);

--Creating the Cursor to get the details of the username, and differrence in End Time and Start Time
CURSOR timediff IS
SELECT username,
       trunc(86400*(end_time-start_time))-60*(trunc((86400*(end_time-start_time))/60)),
       trunc((86400*(end_time-start_time))/60)-60*(trunc(((86400*(end_time-start_time))/60)/60)),
       trunc(((86400*(end_time-start_time))/60)/60)-24*(trunc((((86400*(end_time-start_time))/60)/60)/24)),
       trunc((((86400*(end_time-start_time))/60)/60)/24)
FROM user_action_log;

BEGIN
OPEN timediff;
LOOP
	--Fetching the cursor data into the variables set.
	FETCH timediff INTO username,Seconds,Minutes,Hours,Days;
	EXIT WHEN timediff%NOTFOUND; 
	--Displaying the output in a particular format.
	DBMS_OUTPUT.PUT_LINE('USERNAME: '||username||' Seconds: '||Seconds||' Minutes: '||Minutes||' Hours: '||Hours||' Days: '||Days);
END LOOP;
CLOSE timediff;
END;
/
--ALTERNATELVELY FOR THE USERFRIENDLY FORMAT YOU CAN USE THE STATEMENT AS:
SELECT username,
       trunc(86400*(end_time-start_time))-60*(trunc((86400*(end_time-start_time))/60)) "Seconds",
       trunc((86400*(end_time-start_time))/60)-60*(trunc(((86400*(end_time-start_time))/60)/60)) "Minutes",
       trunc(((86400*(end_time-start_time))/60)/60)-24*(trunc((((86400*(end_time-start_time))/60)/60)/24)) "Hours",
       trunc((((86400*(end_time-start_time))/60)/60)/24) "Days"
FROM user_action_log;
